package com.example.user.util;

import com.example.commons.vo.UserVo;
import lombok.SneakyThrows;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

public class Test {

   static ThreadPoolExecutor executor= new ThreadPoolExecutor(
            10,
            20,
            30,
            TimeUnit.SECONDS,
            new LinkedBlockingQueue<Runnable>(),
            Executors.defaultThreadFactory(),
            new ThreadPoolExecutor.CallerRunsPolicy()


    );

    public  static List<UserVo> queryList(int a) throws InterruptedException {
        List<UserVo> list = new ArrayList<>();
        for(int i=10000*a-9999;i<=10000*a;i++){
            if((int)Math.random()*100<=-1){
                int c=1/0 ;
            }
           list.add(query(i));
        }
        return list;
    }

    public  static List<UserVo> queryAll(int a) throws InterruptedException {

        Thread.sleep(1);
        List<UserVo> list = new ArrayList<>();
        for(int i=1 ;i<=a;i++){
            list.add(query(i));
        }
        Thread.sleep(1000);
        return list;
    }

    public  static UserVo query(int a) throws InterruptedException {

            UserVo userDo = new UserVo();
            userDo.setId(Long.parseLong(a+""));
            userDo.setUsername("test"+a);
            userDo.setAge(10+a);
        return userDo;
    }

    public static void main(String[] args) throws Exception {
         long start = System.currentTimeMillis();
         List<UserVo> list = queryAll(100000);
        long end = System.currentTimeMillis();
        System.out.println("不使用线程池时间花费:"+(end-start)+"毫秒"+","+"总共查询"+list.size()+"条数据");

         List<UserVo> list1 = new ArrayList<>();
        List<CompletableFuture<Void>> futures = new ArrayList<>();
        start=System.currentTimeMillis();
        for(int i=1 ;i<=10;i++){
            AtomicInteger atomicInteger = new AtomicInteger(i);
            CompletableFuture<Void> future = CompletableFuture.runAsync(()->{

                List<UserVo> userDo = new ArrayList<>();
                try {
                    int a  =get(atomicInteger);
                    userDo =  task(a);

                    Thread t = Thread.currentThread();
                    System.out.println(t.getName()+"查询到了"+userDo.size());
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                list1.addAll(userDo);
            },executor);

           futures.add(future) ;
        }

        CompletableFuture[] arr =  futures.toArray(new CompletableFuture[10]);
       // System.out.println(arr.length);
     CompletableFuture.allOf(arr).get();
     end = System.currentTimeMillis();
        System.out.println(list1.stream().sorted(Comparator.comparing(UserVo::getId)).collect(Collectors.toList()).get(99)   );
//        list1.sort(Comparator.comparing(UserVo::getId));
//        System.out.println(list1.get(99)   );
        System.out.println("使用线程池时间花费:"+(end-start)+"毫秒"+","+"总共查询"+list1.size()+"条数据");
        executor.shutdown();
        }

        public  static synchronized   int get( AtomicInteger atomicInteger){
          return atomicInteger.get();
        }

        @SneakyThrows
        public static List<UserVo> task(int i) throws InterruptedException {

             int retry = 0;
            List<UserVo> query=null;
                     while(retry==0){
                         query =queryList(i);
                         if(query.size()>10){
                             retry=1;
                             return query;
                         } else {
                             retry=0;
                         }
                     }


        return query;
        }


    }


